###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Rate.pm.pl -> Rate module                                                   #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Rate;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "Output/Rate.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  my $LANGUAGE = &Standard::GetLanguage("Rate");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my (%RECORD, %INPUT);
  
  $RECORD{'RID'}    = $SL::QUERY{'RID'};
  $RECORD{'RATING'} = $SL::QUERY{'FORM_RATING'};
  $RECORD{'IP'}     = $ENV{'REMOTE_ADDR'};
  
  unless ($RECORD{'RID'})    { &Error::Error("SL", MESSAGE => $LANGUAGE->{'MISSING'}->{'RID'}); }
  unless ($RECORD{'RATING'}) { &Error::Error("SL", MESSAGE => $LANGUAGE->{'MISSING'}->{'RATING'}); }

  if ($RECORD{'RATING'} < 1 || $RECORD{'RATING'} > 10 || $RECORD{'RATING'} % 1) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'INVALID'}->{'RATING'});
  }

  my $Resource = $in{'DB'}->BinarySelect(
    TABLE   => "Resources",
    KEY     => $RECORD{'RID'}
  );
  unless ($Resource) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'INVALID'}->{'RID'});
  }

  my $Category = $in{'DB'}->BinarySelect(
    TABLE   => "Categories",
    KEY     => $Resource->{'CATEGORY'}
  );
  
  if (
    $in{'DB'}->Query(
      TABLE   => "Rates",
      WHERE   => {
        RID => [$RECORD{'RID'}],
        IP  => [$RECORD{'IP'}]
      },
      MATCH   => "ALL",
      COUNT   => 1
    )
  ) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'ALREADY-RATED'});
  }

  #----------------------------------------------------------------------#
  # Inserting data...                                                    #
	
  (
    $in{'DB'}->Insert(
      TABLE   => "Rates",
      VALUES  => {
        RID => $RECORD{'RID'},
        IP  => $RECORD{'IP'}
      }
    )
  ) || &Error::Error("SL", MESSAGE => "Error inserting record into database. $in{'DB'}->{'ERROR'}");

  my $TotalRatings = $in{'DB'}->Query(
    TABLE   => "Rates",
    WHERE   => {
      RID => [$RECORD{'RID'}]
    },
    MATCH   => "ALL",
    COUNT   => 1
  );

  $Resource->{'RATING'} = &Standard::Round(($Resource->{'RATING'} * ($TotalRatings - 1) + $RECORD{'RATING'}) / $TotalRatings);

  (
    $in{'DB'}->Update(
      TABLE   => "Resources",
      VALUES  => {
        RATING  => $Resource->{'RATING'}
      },
      KEY     => $RECORD{'RID'}
    )
  ) || &Error::Error("SL", MESSAGE => "Error updating record in database. $in{'DB'}->{'ERROR'}");

  my $StaticPages = StaticPages->new(DB => $in{'DB'});

  $StaticPages->Update(TYPE => "Detail", INPUT => $Resource)
    || &Error::Error("SL", MESSAGE => "Error updating static pages. $in{'DB'}->{'ERROR'}");

  $INPUT{'RECORD'}   = \%RECORD;
  $INPUT{'RESOURCE'} = $Resource;
  $INPUT{'CATEGORY'} = $Category;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Rate->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);

  return 1;
}

1;